<?php

	$dbname = 'test';
	date_default_timezone_set('PRC'); 
    $filename = date("Y-m-d-h-i");
    echo $filename;
    //创建目录
    $path='C:/wamp/www/ivyproject/Public/backup/'.$filename.'/';
    if (!file_exists($path))
    mkdir(iconv("UTF-8", "GB2312", $path),0755,true);
    
    
    //备份upload文件夹
    //要备份文件夹，需要先删除原来的文件夹，以免造成冲突
    include("deldir.php");    
    include("dir.php");
    $from='C:/wamp/www/ivyproject/Public/uploads';
    $to='C:/wamp/www/ivyproject/Public/backup/'.$filename.'/'.$filename;
    $del = new deldir($to);   
    $file = new copyFile($from, $to);
    
	//备份数据库
	$dump_tool = "C:/wamp/bin/mysql/mysql5.5.20/bin/mysqldump";
    
    $name=$path.$filename.'.sql';
    
	  $con = mysql_connect("localhost","root","");
		if (!$con)
 		 {
 		 die('Could not connect: ' . mysql_error());
		  }
		mysql_select_db("test", $con);
    $sql="insert into termbackup values(NULL,'".$filename."','".$name."')";
	mysql_query($sql,$con);
    
    $command= $dump_tool.' -u root '.$dbname.' > C:/wamp/www/ivyproject/Public/backup/'.$filename.'/'.$filename.'.sql';
    exec($command); 
    //连接数据库，将最近的备份信息存储
    
    echo "备份完成，您可以在恢复中用本次备份的结果恢复数据库以及upload文件夹"; 
	echo ' <script type="text/javascript">
  
function CloseWindow(action) {

        if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);

        else window.close();

    	} 
		
  </script>';

?>